package ahblue.cn.tanzi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import ahblue.cn.tanzi.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 显示文章内容
 */
public class WebViewActivity extends AppCompatActivity {

    private final static String ARG_ARTICLE_ID = "ARG_ARTICLE_ID";

    @BindView(R.id.webViewArticle)
    WebView webviewWv;


    private String articleUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        ButterKnife.bind(this);

        final Intent intent = getIntent();
        articleUri = intent.getStringExtra(ARG_ARTICLE_ID);
        iniData();

    }

    private void iniData(){
        if(!TextUtils.isEmpty(articleUri)){
            webviewWv.getSettings().setJavaScriptEnabled(true);
            webviewWv.getSettings().setDefaultTextEncodingName("UTF-8");
            webviewWv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            //webviewWv.addJavascriptInterface(new JS(), "android");
            webviewWv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webviewWv.getSettings().setUseWideViewPort(true);
            webviewWv.getSettings().setLoadWithOverviewMode(true);
            webviewWv.getSettings().setSupportZoom(true);
            webviewWv.getSettings().setBuiltInZoomControls(true);
            webviewWv.getSettings().setDisplayZoomControls(false);
            webviewWv.setWebViewClient(new WebViewClient() {
                //覆盖shouldOverrideUrlLoading 方法
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });

            /*webviewWv.setWebChromeClient(new WebChromeClient(){
                @Override
                public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                    Uri uri = Uri.parse(message);
                    if (uri != null){
                        if ("js".equals(uri.getScheme())){
                            result.confirm(parseUri(uri));
                        }
                        return true;
                    }
                    return super.onJsPrompt(view, url, message, defaultValue, result);
                }
            });*/
            /*webviewWv.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    String titleStr = view.getTitle();
                    title.setText("");
                    if (!TextUtils.isEmpty(titleStr)) {
                        //title.setText(titleStr);
                    }
                }
            });*/


            webviewWv.loadUrl(articleUri);
        }
    }

    @OnClick(R.id.imageButtonLeft)
    void onClickImageButtonLeft() {
        finish();
    }
}
